// app.js
require('./utils/Page')

//app.js
const baseURL = 'http://localhost:3000'
wx.$format=require('./utils/util')
let $https = require("./utils/request")
wx.$https =$https
wx.get = $https.get
wx.baseURL =baseURL
wx.put = $https.put
wx.post = $https.post
wx.delete = $https.delete
App({
  onLaunch() {
    // 展示本地存储能力
    const logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)

    // 登录
    wx.login({
      success: res => {
        console.log(res.code)
        wx.get('/wxlogin?code=' + res.code).then(res=>{
          wx.userInfo = res
          wx.token = res.token
          if(wx.waitLoginPages.length){
            for(let i =0; i<wx.waitLoginPages.length;i++) {
              let page = wx.waitLoginPages[i]
              page.onLoad.call(page.context,page.options)
            }
          }
          console.log(wx.userInfo)
          if(wx.userInfo.nickName == ''|| wx.userInfo.nickName==null){
            wx.showModal({
              title: '提示',
              content: '我们希望获得您的个人信息，以便为您提供更好的服务！',
              cancelText:'稍后再说',
              success(res) {
                if (res.confirm) {
                  console.log('用户点击确定')
                  wx.getUserProfile({
                    desc: '获取头像和名称', 
                  }).then(res => {
                    //授权成功后缓存到全局wx.userinfo
                    wx.userInfo.nickName = res.userInfo.nickName,
                    wx.userInfo.avatarUrl = res.userInfo.avatarUrl
                    wx.put('/wx/member',wx.userInfo).then(res=>{
                      console.log(res)
                    })
                  })
                  .catch(err => {
                    console.log("授权中断 用户信息未录入 登录失败！",wx.userInfo)
                  })
                } else if (res.cancel) {
                  console.log('用户点击取消，用户没有登录')
                  console.log(wx.userInfo)
                }
              }
            })
          }
        })
       
        
       
      }
    })
  },
  globalData: {
    userInfo: null
  }
})
